package airport.client.widget.accueil;

import airport.client.Airport;
import airport.client.utils.Constantes;
import airport.shared.beans.PanneauBean;
import airport.shared.beans.UserBean;
import airport.shared.service.AirportService;
import airport.shared.service.AirportServiceAsync;
import airport.shared.service.PanneauService;
import airport.shared.service.PanneauServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;

/**
 * Created by Romain on 28/02/15.
 */
public class AccueilWB extends Airport {

	FlowPanel panel = new FlowPanel();

	Label titre = new Label();
	Label infoVol = new Label();

	Label nomHotel = new Label();
	Label adresseHotel = new Label();
	Label standingHotel = new Label();
	Label tarifHotel = new Label();

	Label date_departVol = new Label();
	Label date_arriveeVol = new Label();
	Label lieu_departVol = new Label();
	Label lieu_arriveeVol = new Label();
	Label tarifVol = new Label();

	FlowPanel titrePanel = new FlowPanel();
	FlowPanel volPanel = new FlowPanel();
	FlowPanel titrePanelRow = new FlowPanel();
	FlowPanel volPanelRow = new FlowPanel();

	public AccueilWB() {
		init();
	}

	private void init() {
		initWidget(panel);
        checkLogin();
    }

    public void checkLogin() {
        final AirportServiceAsync connexionIface;
        connexionIface = GWT.create(AirportService.class);

        connexionIface.checkLogin(new AsyncCallback<UserBean>() {

            @Override
            public void onFailure(Throwable caught) {
                infoDanger(caught.toString());
            }

            @Override
            public void onSuccess(UserBean result) {
                if (result != null) {
                    labelTitre.getElement().setInnerHTML("Bienvenue " + result.getPrenom() + " " + result.getNom());
                    loadPanneaux();
                } else {
                    initbouton(false);
                    loadPanneaux();
                }
            }
        });
    }

	private void loadPanneaux() {
		final PanneauServiceAsync panneauIface;
		panneauIface = GWT.create(PanneauService.class);

		infoDangerHide();
        panneauIface.selectAllByDate(new AsyncCallback<PanneauBean>() {
            @Override
            public void onFailure(Throwable caught) {
                infoDanger(caught.getMessage());
            }

            @Override
            public void onSuccess(PanneauBean result) {

                panel.addStyleName("arrondie panneau" + result.getImage());
                if (result == null) {
                    titre.setText("Prochainement...");
                } else {
                lieu_departVol.setText(String.valueOf(result.getVol().getLieu_D()));
                lieu_arriveeVol.setText(String.valueOf(result.getVol().getLieu_A()));
                date_departVol.setText(String.valueOf(result.getVol().getDate_D()));
                date_arriveeVol.setText(String.valueOf(result.getVol().getDate_A()));
                tarifVol.setText(String.valueOf(result.getVol().getTarif()));

                nomHotel.setText(result.getHotel().getNom());
                adresseHotel.setText(result.getHotel().getAdresse());
                standingHotel.setText(String.valueOf(result.getHotel().getStanding()));
                tarifHotel.setText(String.valueOf(result.getHotel().getTarif()));


                // Formatage infos vol
                long duree = (Math.abs(result.getVol().getDate_A().getTime() - result.getVol().getDate_D().getTime()));
                duree /= (24 * 60 * 60 * 1000);

                String date_depart = DateTimeFormat.getFormat("dd/MM/yyyy").format(result.getVol().getDate_D());
                String date_arrivee = DateTimeFormat.getFormat("dd/MM/yyyy").format(result.getVol().getDate_A());
                String tarif = tarifVol.getText().split("\\.")[0];
                String standing = new String();

                for (int i = 0; i < result.getHotel().getStanding(); i++) standing += "★";

                infoVol.setText(
                        "Pour un voyage inoubliable" + "\n" +
                                "À l' Hôtel " + standing + " " + nomHotel.getText() + "\n\n" +
                                "Départ de " + lieu_departVol.getText() + " le " + date_depart + "\n" +
                                "Retour de " + lieu_arriveeVol.getText() + " le " + date_arrivee + "\n\n" +
                                "À partir de " + tarif + "€"

                );

                volPanel.add(infoVol);
                    volPanel.setStyleName("panneau-info" + result.getImage() + " span6 offset1 secondary");

                // Titre
                    titre.setText("Découvrez\n\t" + lieu_arriveeVol.getText());
                }
                titrePanel.setStyleName("panneau-titre span6 offset3 primary");
                titrePanel.add(titre);

                titrePanelRow.setStyleName(Constantes.ROW_FLUID);
                titrePanelRow.add(titrePanel);

                volPanelRow.setStyleName(Constantes.ROW_FLUID);
                volPanelRow.add(volPanel);

                panel.add(titrePanelRow);
                panel.add(volPanelRow);
            }
        });
    }

}